/**
 * Created by yanwe on 2016/7/4.
 */
/**
 * Created by yanwe on 2016/6/26.
 */
(function(){
    'use strict';
    app.controller('DeliverGoodsOrderListController',[
        '$scope','deliverGoodsOrderService','i18nService','$state','$stateParams',function($scope, deliverGoodsOrderService,i18nService,$state,$stateParams){
            $(".datetimepicker_").datetimepicker({
                format: 'yyyy-mm-dd hh:ii:ss',
                language:"cn",
                weekStart: 1,
                todayBtn:  "linked",
                autoclose: true,
                startView: 2,
                todayHighlight: true,
                endDate : new Date()
            });
            $scope.filterOptions = {
                condition:{
                    goodsName: "",
                    startTime:"",
                    endTime:"",
                    label:""
                },
                useExternalFilter: true
            };
            console.log($scope.filterOptions);
            i18nService.setCurrentLang('zh-cn');
            $scope.totalServerItems = 0;
            console.log($stateParams.pageSize,$stateParams.currentPage);
            $scope.pagingOptions = {
                pageSizes: [250, 500, 1000],
                pageSize: 250,
                currentPage: 1
            };
            $scope.setPagingData = function (data, page, pageSize) {
                $scope.myData = data.data;
                $scope.totalServerItems = data.totalServerItems;
                if (!$scope.$$phase) {
                    $scope.$apply();
                }
            };
            
            $scope.getPagedDataAsync = function (pageSize, page, condition) {
                setTimeout(function () {
                    if (condition) {
                        deliverGoodsOrderService.getOrderList(condition.goodsName,condition.startTime,
                            condition.endTime,condition.label,page).success(function (largeLoad) {
                            $scope.setPagingData(largeLoad, page, pageSize);
                        });
                    } else {
                        deliverGoodsOrderService.getOrderList('','','','',page).success(function (largeLoad) {
                            $scope.setPagingData(largeLoad, page, pageSize);
                        });
                    }
                }, 100);
            };
            
            $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);
            $scope.$watch('pagingOptions', function (newVal, oldVal) {
                if (newVal !== oldVal && newVal.currentPage !== oldVal.currentPage) {
                    $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.condition);
                }
            }, true);
            $scope.$watch('filterOptions', function (newVal, oldVal) {
                if (newVal !== oldVal) {
                    $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.condition);
                }
            }, true);
            $scope.showMe = function ($this) {
                $state.go('app.order.deliver_goods_invoices_detail',{orderId:$this.row.entity.invoiceOrderId});
                //console.log($this.row);
            };
            $scope.gridOptions = {
                paginationPageSizes: [15, 20, 25],
                paginationPageSize: 15,
                enableColumnResizing: true,
                enableGridMenu: true,
                columnDefs: [
                    {name: 'goodsNames', pinnedLeft: true, displayName: '商品名称', width: 200},
                    {name: 'deliveryTime', displayName: '发货时间', width: 190},
                    {name: 'status', displayName: '发货状态', width: 90},
                    {name: 'expressCode', displayName: '快递单号', width: 100},
                    {name: 'consignerName', displayName: '发货人姓名', width: 100},
                    {name: 'consignerTelphone', displayName: '发货人电话', width: 100},
                    {name: 'createTime', displayName: '订单时间', width: 190},
                    {
                        name: '详情', width: 110,
                        pinnedRight: true,
                        cellTemplate: '<button class="btn btn-mini btn-primary table-btn" ng-click="grid.appScope.showMe(this)">详情</button>'
                    }
                ],
                data: 'myData'
            };
            $scope.searchByFilter=function(){
                $scope.filterOptions.condition=$scope.condition;
                console.log($scope.filterOptions);
                $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.condition);
            };
        }
    ]);

    app.controller('DeliverGoodsOrderDetailController',[
        '$scope','$stateParams','$state','deliverGoodsOrderService',function($scope, $stateParams,$state,deliverGoodsOrderService){
            $scope.orderId=$stateParams.orderId;
            deliverGoodsOrderService.getOrderDetail($scope.orderId).then(function(success){
                $scope.orderDetail=success.data;
            });
            $scope.backToOrderList=function(){
                $state.go('app.order.deliver_goods_invoices_list',{currentPage:$stateParams.currentPage,pageSize:$stateParams.pageSize});
            }
            $scope.createInvoice=function(){
                $state.go('app.order.deliver_goods_invoices_create',{orderId:$scope.orderId});
            }
        }
    ]);
    app.controller('CreateInvoiceController',[
        '$scope','$stateParams','$state','deliverGoodsOrderService',function($scope, $stateParams,$state,deliverGoodsOrderService){
            $(".datetimepicker_").datetimepicker({
                format: 'yyyy-mm-dd hh:ii:ss',
                language:"cn",
                weekStart: 1,
                todayBtn:  "linked",
                startView: 2,
                autoclose: true,
                todayHighlight: true,
                endDate : new Date()
            });
            $scope.orderId=$stateParams.orderId;
            deliverGoodsOrderService.getPurchaseGoodsList().then(function(success){
                $scope.purchaseGoodsList=success.data;
            });
            deliverGoodsOrderService.getTransferGoodsList().then(function(success){
                $scope.transferGoodsList=success.data;
            });
            $scope.backToOrderDetail=function(){
                $state.go('app.order.deliver_goods_invoices_detail',{orderId:$scope.orderId});
            }
            $scope.invoice={
                deliveryWay:"1",
                deliveryTime:'2016-09-25 11:00:00',
                expressCode:"SM201609250001",
                consignerName:"chenyong",
                consignerTelphone:'13717791628',
                status:"1",
                consigneeId:"",
                consigneeName:"",
                consigneeTel:"",
                consigneeAddress:"",
                consigneePostcode:"",
                goodsList:""
            };
            //选中的收货人信息id
            var selectedIds=[];
            //选中的商品信息
            var selectGoodsInfoArr=[];
            $scope.selectGoods=function(target){
                var consigneeId= target.goods.consigneeId;
                if(target.checkFlag){
                    if($scope.invoice.consigneeId==""){
                        selectedIds.push(consigneeId);
                        selectGoodsInfoArr.push(target.goods);
                        $scope.invoice.consigneeId=consigneeId;
                        $scope.invoice.consigneeName=target.goods.consigneeName;
                        $scope.invoice.consigneeTel=target.goods.consigneeTel;
                        $scope.invoice.consigneeAddress=target.goods.consigneeAddress;
                        $scope.invoice.consigneePostcode=target.goods.consigneePostcode;
                    }else{
                        if($scope.invoice.consigneeId!=target.goods.consigneeId){
                            target.checkFlag=false;
                            alert("发货地址不一样，不能一起发货");
                        }else{
                            selectedIds.push(consigneeId);
                            selectGoodsInfoArr.push(target.goods);
                        }
                    }
                }else{
                    var pos=selectedIds.indexOf(consigneeId);
                    if(pos!=-1){
                        selectedIds.splice(pos,1);
                    }
                    if(selectedIds.length==0){
                        $scope.invoice.consigneeId="";
                        $scope.invoice.consigneeName="";
                        $scope.invoice.consigneeTel="";
                        $scope.invoice.consigneeAddress="";
                        $scope.invoice.consigneePostcode="";
                    }
                    var index=selectGoodsInfoArr.indexOf(target.goods);
                    if(index!=-1){
                        selectGoodsInfoArr.splice(index,1);
                    }
                }
            };

            $scope.createInvoice=function(){
                $scope.invoice.goodsList=JSON.stringify(selectGoodsInfoArr);
                console.log($scope.invoice);
                deliverGoodsOrderService.delivery($scope.invoice);
                $state.go('app.order.deliver_goods_invoices_list',{currentPage:$stateParams.currentPage,pageSize:$stateParams.pageSize});
            }
        }
    ]);
})();
